import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/views/one-level/login'
import Register from '@/views/one-level/register'
import Layout from '@/views/one-level/layout'
import Consult from '@/views/two-level/ConsultVue'
import Home from '@/views/two-level/HomeVue'
import Look from '@/views/two-level/LookHouse'
import My from '@/views/two-level/MyVue'
import Favorites from '@/views/one-level/FavoritesList'
import UserHouser from '@/views/one-level/UserHouser'
import CityList from '@/views/one-level/CityList'
import { getToken } from '@/utils/storage'
import Housing from '@/views/one-level/PublishingHousing'
import SearchArea from '@/views/one-level/SearchArea'
import BaiduMap from '@/views/one-level/BaiduMap'
Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    component: Login
  },
  {
    path: '/register',
    component: Register
  },
  {
    path: '/',
    component: Layout,
    redirect: '/home',
    children: [
      {
        path: '/consult',
        component: Consult
      },
      {
        path: '/home',
        component: Home
      },
      {
        path: '/look',
        component: Look
      },
      {
        path: '/my',
        component: My
      }
    ]
  },
  {
    path: '/favorites',
    component: Favorites
  },
  {
    path: '/userhouser',
    component: UserHouser
  },
  {
    path: '/citylist',
    component: CityList
  },
  {
    path: '/housing',
    component: Housing
  },
  {
    path: '/searchArea',
    component: SearchArea
  },
  {
    path: '/baidumap',
    component: BaiduMap
  }
]

const router = new VueRouter({
  routes
})
/* 白名单 */
const whileList = ['/login', '/register', '/my', '/home', '/look', '/consult']
router.beforeEach((to, from, next) => {
  if (getToken()) {
    next()
  } else {
    if (whileList.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
})

export default router
